import 'package:flutter/material.dart';
import 'package:hairen_app/pages/plug/func.dart';
import 'package:hairen_app/server/server.dart';
import 'package:hairen_app/server/saveInfo.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';
import 'package:flutter/cupertino.dart';
import 'package:hairen_app/pages/player/player.dart';
import 'dart:async';
import 'login.dart';
import '../plug/wlist.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:hairen_app/pages/plug/drawer.dart';

class FavoritesList extends StatefulWidget {
  int fid;

  FavoritesList(this.fid);

  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return new _FavoritesList();
  }
}

enum WhyFarther { harder, smarter, selfStarter, tradingCharter }

class _FavoritesList extends State<FavoritesList> {
  Map userInfo = {};
  String token = "";
  List<Widget> list = <Widget>[];
  int page = 1;
  int total = 1;
  int last_page = 1;
  Future getFavorites = Server.getFavorite();

  getInfo() {
    SaveInfo.getUserToken().then((Map ret) {
      setState(() {
        userInfo = ret['userInfo'];
        token = ret['token'];
      });
    });
  }
  
 Widget rowWidget(Map data){
    return new Expanded(
        flex: 1,
        child: Container(
            margin: EdgeInsets.only(
                left: 7.5, right: 7.5, bottom: 7.5, top: 7.5),
            child: new InkWell(
              child: new Column(
                children: <Widget>[
                  new Container(
                    height: 200.0,
                    child: new Stack(
                      children: <Widget>[
                        new Material(
                          borderRadius:
                          BorderRadius.all(Radius.circular(8.0)),
                          child: new Image.network(
                            data['logo'],
                            fit: BoxFit.cover,
                            height: 200.0,
                            width: 300.0,
                          ),
                        ),
                        new Container(
                          margin: EdgeInsets.only(top: 170.0),
                          height: 30.0,
                          child: new Material(
                            color: Color.fromRGBO(14, 14, 14, 0.51),
                            child: new Row(children: <Widget>[
                              new Expanded(
                                flex: 1,
                                child: new Container(
                                  padding: EdgeInsets.only(left: 10.0),
                                  child: new Row(
                                    children: <Widget>[
                                      new Icon(
                                        Icons.category,
                                        color: Colors.white,
                                        size: 12.0,
                                      ),
                                      new Container(
                                        width: 5.0,
                                      ),
                                      new Text(
                                        data['type'],
                                        style: new TextStyle(
                                            color: Colors.white,
                                            fontSize: 12.0),
                                      )
                                    ],
                                  ),
                                ),
                              ),
                              new Expanded(
                                flex: 1,
                                child: new Container(
                                    padding: EdgeInsets.only(right: 10.0),
                                    child: new Row(
                                      mainAxisAlignment:
                                      MainAxisAlignment.end,
                                      children: <Widget>[
                                        new Icon(
                                          Icons.access_time,
                                          color: Colors.white,
                                          size: 12.0,
                                        ),
                                        new Container(
                                          width: 5.0,
                                        ),
                                        new Text(
                                          func.diffTime(
                                              DateTime.parse(
                                                  data['update']),
                                              DateTime.now()),
                                          style: new TextStyle(
                                              color: Colors.white,
                                              fontSize: 12.0),
                                        )
                                      ],
                                    )),
                              ),
                            ]),
                            borderRadius: BorderRadius.only(
                                bottomLeft: Radius.circular(8.0),
                                bottomRight: Radius.circular(8.0),
                                topLeft: Radius.circular(0.0),
                                topRight: Radius.circular(0.0)),
                          ),
                        ),
                      ],
                    ),
                    decoration: new BoxDecoration(
//                  border: Border.all(
//                      width: 1.0, color: Color.fromRGBO(239, 239, 239, 1.0)),
                      color: Colors.blue,
                      borderRadius:
                      BorderRadius.all(Radius.circular(8.0)),
//                  image: new DecorationImage(
//                    image: new NetworkImage(
//                        'https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=4097200241,3195669789&fm=26&gp=0.jpg'),
//                    fit: BoxFit.cover,
//                  ),
                    ),
                  ),
                  new Container(
                    height: 40.0,
                    child: new ListTile(
                      title: new Text(
                        data['title'],
                        maxLines: 1,
//                    textAlign: TextAlign.center,
                        style: new TextStyle(fontSize: 16.0),
                      ),
//                  subtitle: new Text('收账不成，无处可归'),
                    ),
                  )
                ],
              ),
              onTap: () {
                Navigator.of(context).push(new CupertinoPageRoute(
                    builder: (context) =>
                    new PlayerPage(mid: int.parse(data['id']))));
              },
            )));
  }
  
  getList() {
    Server.getFavoriteList(data: {
      'fid': widget.fid.toString(),
      'page': page.toString(),
    }).then((ret) {
      setState(() {
        page = ret['data']['current_page'];
        total = ret['data']['total'];
        last_page = ret['data']['last_page'];
        var data = ret['data']['data'];
        for (var i = 0; i < data.length; i++) {
          list.add(rowWidget(data));
        }
        print(list);
      });
    });
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();

  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      appBar: AppBar(
        elevation: 0.0,
        title: new Text("我的收藏"),
        actions: <Widget>[

        ],
      ),
      body: new ListView(
            children: <Widget>[
                new FutureBuilder(
              future: Server.getFavoriteList(data: {
                'fid': widget.fid.toString(),
                'page': page.toString(),
              }),
              builder: (context, ret) {
                if(ret.hasData){
                  page = ret.data['data']['current_page'];
                  total = ret.data['data']['total'];
                  last_page = ret.data['data']['last_page'];
                  var data = ret.data['data']['data'];
                  for (var i = 0; i < data.length; i++) {
                    list.add(rowWidget(data[i]));
                  }
                  return new WlistPage(
                    children: list,
                    column: 2,
                  );
                }else{
                  return new Container(
                    height: 100.0,
                    child: new SpinKitWave(
                      color: Color.fromRGBO(251, 114, 153, 1.0),
                      size: 30.0,
                    ),
                  );
                }

              })
            ],
        ),
    );
  }
}
